﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class Clocks
    {

        public Clocks()
        {
            int[][] swit = new int[][] {
                new int[] {0, 1, 2},
                new int[] {4, 10, 14, 15},
                new int[] {0, 4, 5, 6, 7},
                new int[] {0, 2, 14, 15},
                new int[] {3, 14, 15},
                new int[] {4, 5, 7, 14, 15},
                new int[] {1, 2, 3, 4, 5}
            };
            int[][] destSwit = new int[][] {
                new int[] {3, 7, 9, 11},
                new int[] {6, 7, 8, 10, 12},
                new int[] {3, 4, 5, 9, 13}
            };
            
            int testCase = GetConsole.GetInt();
            int[] watch = new int[16];
            while (testCase-- > 0)
            {
                int[] click = new int[7];
                for (int i = 0; i < 16; i++)
                {
                    int input = GetConsole.GetInt();
                    switch (input)
                    {
                        case 3: watch[i] = 1; break;
                        case 6: watch[i] = 2; break;
                        case 9: watch[i] = 3; break;
                        case 12: watch[i] = 0; break;
                    }
                }
                //click[4] = watch[8];
                //click[1] = watch[11];
                //click[9] = watch[13];
                //이미 결정난 버튼들 누르기
                int[] destClick = new int[3];
                destClick[0] = (4 - watch[11]) % 4;
                destClick[1] = (4 - watch[8]) % 4;
                destClick[2] = (4 - watch[13]) % 4;
                Push(destClick, watch, destSwit);
                if (Check(watch))
                {
                    Console.WriteLine(watch.Sum());
                    continue;
                }
                int minValue = Int32.MaxValue;
                
                while (Next(click, 3))
                {
                    int[] pushWatch = (int[])watch.Clone();
                    Push(click, pushWatch, swit);
                    if (Check(pushWatch))
                    {
                        int sum = click.Sum();
                        if (sum < minValue)
                        { minValue = sum; }
                    }
                }
                Console.WriteLine((minValue < Int32.MaxValue) ? minValue + destClick.Sum() : -1);
                
            }
        }
        public bool Check(int[] watch)
        {
            for (int i = 0; i < watch.Length; i++)
            {
                if (watch[i] != 0)
                { return false; }
            }
            return true;
        }
        public void Push(int[] click, int[] watch, int[][] swit)
        {
            for (int i = 0; i < click.Length; i++)
            {
                int clickNum = click[i];
                foreach (int clickSwit in swit[i])
                {
                    watch[clickSwit] += clickNum;
                    if (watch[clickSwit] > 3)
                    { watch[clickSwit] -= 4; }
                }
            }
        }
        public bool Next(int[] nowSize, int endSize)
        {
            nowSize[0]++;
            int count = nowSize.Length - 1;
            for (int i = 0; i < count; i++)
            {
                if (nowSize[i] > endSize)
                {
                    nowSize[i] = 0;
                    nowSize[i + 1]++;
                }
                else
                {
                    break;
                }
            }
            if (nowSize[count] > endSize)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
